#include "ObliqueTileStatistic.h"
#include "ObliqueUtil.h"

using namespace OCPP;
using namespace OCPP::Cesium;

Json::Value ObliqueTileStatistic::toJson()
{
	Json::Value value;
	value["nodeDepth"] = nodeDepth;
	value["maxNumGeodeInNode"] = maxNumGeodeInNode;
	value["numGeode"] = numGeode;

	Json::Value numGeodePerLevelObj;

	StringStream sstream1;
	for (auto i = 0; i < numGeodePerLevel.size(); i++)
	{
		if (i != 0)
		{
			sstream1 << ",";
		}
		sstream1 << std::setw(2) << std::setfill('0') << numGeodePerLevel[i];
	}

	value["numberGeodePerLevel"] = sstream1.str();

	{
		StringStream sstream2;
		for (auto i = 0; i < numPagedLodPerLevel.size(); i++)
		{
			if (i != 0)
			{
				sstream2 << ",";
			}
			sstream2 << std::setw(2) << std::setfill('0') << numPagedLodPerLevel[i];
		}

		value["numPagedLodPerLevel"] = sstream2.str();
	}

	{
		String sstream;
		for (auto& [first, second] : tileLevelMap)
		{
			if (sstream.size() != 0)
			{
				sstream += ",";
			}
			sstream += std::to_string(first);
		}

		value["worldLevels"] = sstream;
	}

	return value;
}

void ObliqueTileStatistic::fromJson(Json::Value& value)
{
	nodeDepth = value["nodeDepth"].asUInt();
	maxNumGeodeInNode = value["maxNumGeodeInNode"].asUInt();
	numGeode = value["numGeode"].asUInt();

	String numberGeodePerLevelStr = value["numberGeodePerLevel"].asString();
	StringVector sv1 = StringUtil::split(numberGeodePerLevelStr, ",");
	for (auto& numStr : sv1)
	{
		numGeodePerLevel.push_back(std::stoi(numStr));
	}

	String numPagedLodPerLevelStr = value["numPagedLodPerLevel"].asString();
	StringVector sv2 = StringUtil::split(numPagedLodPerLevelStr, ",");
	for (auto& numStr : sv2)
	{
		numPagedLodPerLevel.push_back(std::stoi(numStr));
	}

	{
		String worldLevelStr = value["worldLevels"].asString();
		StringVector sv2 = StringUtil::split(worldLevelStr, ",");
		for (auto& str : sv2)
		{
			tileLevelMap[std::stoi(str)] = 0;
		}
	}
}
